package com.yc.utile;

import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;

public class StringUtil {
    public static String toString(Object obj) {
        if(null==obj) {
            return "";
        }
        return obj.toString();
    }
    public static boolean checkNull(String...params) {
        if(null==params || params.length<=0 ||params.equals("")) {
            return true;
        }
        for(String param:params) {
            if(null==param||"".equals(param)) {
                return true;
            }
        }
        return false;
    }
    public static boolean dateStrIsValid(String rawDateStr) {

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-hh");
        TemporalAccessor date = null;
        try {
            date = dateTimeFormatter.parse(rawDateStr);
            return rawDateStr.equals(dateTimeFormatter.format(date));
        } catch (DateTimeParseException e) {
            return false;
        }

    }
    public static boolean checkNumber(String...params) {
        for(String param:params) {
            if(!param.matches("[0-9]+.?[0-9]*")) {
                return false;
            }
        }
        return true;
//		strResult = param.matches("-?[0-9]+.?[0-9]*");  //全部数字，正则表达式
//		strResult = param.matches("[0-9]+.?[0-9]*");  //只能匹配正数
    }
    public static boolean checkIntNumber(String...params) {
        for(String param:params) {
            if(!param.matches("[0-9]*")) {
                return false;
            }
        }
        return true;
    }

    /**
     * 将字符串数组转化为整形数组
     * @param strArr
     * @return
     */
    public static int[] toIntArrays(String[] strArr ) {
        int[] intArr=new int[strArr.length];
        for (int i = 0; i < strArr.length; i++) {
            int number = Integer.parseInt(strArr[i]);
            intArr[i] = number;
        }
        return intArr;
    }
    /**
     * 将obj对象转化为字符串对象
     * @param obj
     * @return
     */
    public static String objToString(Object obj) {
        if(null==obj) {
            return "";
        }
        return obj.toString();
    }
    public static boolean isNull(String ... params) {
        if(null==params || params.length<=0) {
            return true;
        }
        for(String s:params) {
            if(null==s || "".equals(s)) {
                return true;
            }
        }
        return false;
    }

    /**
     *
     * @param str
     * @param regex
     * @param index
     * @return
     */
    public static String splitToArrayAndGetElement(String str,String regex,int index) {
        String [] strArr=new String[1024];
        strArr=str.split(regex);
        String element=strArr[index];
        return element;
    }
    //字符串进行非空判断/
    public static boolean isNotNull(String...info) {
        if(null==info||"".equals(info)) {
            return false;
        }

        for(String s:info) {
            if(null==s ||"".equals(s)) {
                return false;
            }
        }

        return true;
    }

}
